import { getUrgentInfo, updateUrgent } from '../../api/app'
import { testContract } from '../../api/contract'
import { goToContract } from '../../utils/util'
const app = getApp()
Page({
    data: {
        data: [],
        emergencyContact:'',
        emergencyContactPhone:'', 
        emergencyContactAddr:'',
        nowAddress:'',
        emergencyhjdz:'',
        emergencyyhkh:'',
        emergencyyhkkhh:'',
        id:'', // true 则是逻辑进入 false 则是 主动进入
        isPhone:'',
        isName:'',
        isAddr:'',
        isnowAddress:'',
        ishjdz:'',
        isyhkh:'',
        isyhkkhh:'',
        counterNum:0
    },
    async onLoad(options){
        console.log(options,'optionsoptionsoptionsoptions');
        // phone=true&name=true&addr=true
        if(options.id){
            const result = await testContract({
                id:options.id
            })
            console.log(result,'resultresultresultresultresult');
            this.setData({
                id:options.id,
                isPhone:result.emergencyContactPhone,
                isName:result.emergencyContact,
                isAddr:result.emergencyContactAddr,
                ishjdz:result.permanentResidenceAddress,
                isnowAddress:result.nowAddress,
                isyhkh:result.bankCardNo,
                isyhkkhh:result.openBankAddress,
            })
            console.log(result, 'resultresult')
        }
        console.log(options)
    },
    onShow(){
        console.log(this.data.id,'idid');
        if(this.data.counterNum === 1){
            this.setData({
                counterNum:0
            })
            wx.switchTab({
                url:'/pages/clock/clock'
            })
        }
        this.getUrgentInfo()
    },
    async getUrgentInfo(){
        const result = await getUrgentInfo()
        console.log(result, 'getUrgentInfo')
        this.setData({
            emergencyContact:result.emergencyContact,
            emergencyContactPhone:result.emergencyContactPhone,
            emergencyContactAddr:result.emergencyContactAddr,
            nowAddress:result.nowAddress,
            emergencyhjdz:result.permanentResidenceAddress,
            emergencyyhkh:result.bankCardNo,
            emergencyyhkkhh:result.openBankAddress,
        })
    },
    async onUpdateUrgent(){
        //  isPhone:result.emergencyContactPhone,
        //  isName:result.emergencyContact,
        //  isAddr:result.emergencyContactAddr
        const regularAddr = /^[u4e00-u9fa5a]/ // 家庭地址开通只能输入中文校验
        const regularPhone = /^(?:(?:\+|00)86)?1\d{10}$/ // 正则来源地址 https://blog.csdn.net/weixin_62277266/article/details/125525477
        const regularName = /[^\u4E00-\u9FA5]/g // 检验只允许输入中文
        const regularBankcardNo = /^[1-9]\d{9,29}$/ // 检验银行卡
        if((this.data.isName==='true' && !this.data.emergencyContact) || (!this.data.id && !this.data.emergencyContact)){
            app.toast('请输入姓名')
            return
        }
        if((this.data.isName==='true' && regularName.test(this.data.emergencyContact)) || (!this.data.id && regularName.test(this.data.emergencyContact))){
            app.toast('姓名只允许输入中文')
            return
        }
        if((this.data.isPhone==='true' && !this.data.emergencyContactPhone) || (!this.data.id && !this.data.emergencyContactPhone)){
            app.toast('请输入手机号')
            return
        }
        if((this.data.isPhone==='true' && !regularPhone.test(this.data.emergencyContactPhone)) || (!this.data.id && !regularPhone.test(this.data.emergencyContactPhone))){
            app.toast('请输入正确的手机号格式')
            return
        }
        if((this.data.isAddr==='true' && !this.data.emergencyContactAddr) || (!this.data.id && !this.data.emergencyContactAddr)){
            app.toast('请输入紧急联系人详细住址')
            return
        }
        if((this.data.isAddr==='true' && regularAddr.test(this.data.emergencyContactAddr)) || (!this.data.id && regularAddr.test(this.data.emergencyContactAddr))){
            app.toast('紧急联系人详细住址开头只允许输入中文')
            return
        }
        if((this.data.isnowAddress==='true' && !this.data.nowAddress) || (!this.data.id && !this.data.nowAddress)){
            app.toast('请输入现住地址')
            return
        }
        if((this.data.isnowAddress==='true' && regularAddr.test(this.data.nowAddress)) || (!this.data.id && regularAddr.test(this.data.nowAddress))){
            app.toast('现住地址开头只允许输入中文')
            return
        }
        if((this.data.ishjdz==='true' && !this.data.emergencyhjdz) || (!this.data.id && !this.data.emergencyhjdz)){
            app.toast('请输入户籍地址')
            return
        }
        if((this.data.ishjdz==='true' && regularAddr.test(this.data.emergencyhjdz)) || (!this.data.id && regularAddr.test(this.data.emergencyhjdz))){
            app.toast('户籍地址开头只允许输入中文')
            return
        }
        if((this.data.isyhkkhh==='true' && !this.data.emergencyyhkkhh) || (!this.data.id && !this.data.emergencyyhkkhh)){
            app.toast('请输入银行卡开户行地址')
            return
        }
        if((this.data.isyhkkhh==='true' && regularAddr.test(this.data.emergencyyhkkhh)) || (!this.data.id && regularAddr.test(this.data.emergencyyhkkhh))){
            app.toast('银行卡开户行地址开头只允许输入中文')
            return
        }
        if((this.data.isyhkh==='true' && !this.data.emergencyyhkh) || (!this.data.id && !this.data.emergencyyhkh)){
            app.toast('请输入银行卡号')
            return
        }
        if((this.data.isyhkh==='true' && !regularBankcardNo.test(this.data.emergencyyhkh)) || (!this.data.id && !regularBankcardNo.test(this.data.emergencyyhkh))){
            app.toast('请输入正确的银行卡号')
            return
        }
        const result = await updateUrgent({
            emergencyContact:this.data.emergencyContact,
            emergencyContactPhone:this.data.emergencyContactPhone,
            emergencyContactAddr:this.data.emergencyContactAddr,
            nowAddress:this.data.nowAddress,
            permanentResidenceAddress:this.data.emergencyhjdz,
            bankCardNo:this.data.emergencyyhkh,
            openBankAddress:this.data.emergencyyhkkhh,
        })
        console.log(result, 'result')
        if(!this.data.id){
            app.toast('编辑成功')
            setTimeout(()=>{wx.navigateBack()},1000)
        }
        if(this.data.id){
            this.setData({
                counterNum:1
            })
            const res = await testContract({
                id:this.data.id
            })
            goToContract(res.flowPath, res.id)
            console.log(res, 'resres')
        }
    },
})